'''
    删除链表的倒数第n个节点，与返回链表的倒数第k个节点类似的题目(LCR140)
'''

class ListNode:
    def __init__(self,val = 0,next = None):
        self.val = val
        self.next = next
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        t = ListNode(-1,head)
        x = self.trainingPlan(t,n+1)
        x.next = x.next.next
        return t.next



    def trainingPlan(self, head: Optional[ListNode], cnt: int) -> Optional[ListNode]:
        t = ListNode()
        t = head
        p = t
        q = t
        for i in range(cnt):
            p = p.next
        while p:
            q = q.next
            p = p.next
        return q   